from dotenv import dotenv_values
from flask import Flask
from flask_socketio import SocketIO
from flask_siwadoc import SiwaDoc
from src.config.env import CONFIG

app = Flask(__name__, static_folder="imgresource")
socketio = SocketIO(app, cors_allowed_origins='*')
siwa = SiwaDoc(app, title="Flask Siwadoc", description="一个自动生成openapi文档的库", version="2.0")

def CreateApp():
    # 选择运行模式
    type = dotenv_values('.flaskenv').get('FLASK_ENV') or 'dev'
    # 加载配置信息
    app.config.from_object(CONFIG[type])
    from src.init.init_db import init_db
    # 初始化db
    init_db(app)
    from src.init.init_api import init_api
    init_api(app)
    from src.init.init_path import init_path
    init_path()

    # 配置网站资源存放位置
    app.static_folder = app.config["UPLOAD_PATH"][1:]

    return app
